﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CSRedis;
using DataSave.ConfigurationBase;

namespace DataSave.RedisClient
{
    public class RedisService : IRedisService
    {
        public static string _redisCon { get; set; }

        public RedisService(IConfigBase config)
        {
            _redisCon = config.GetRedisCon();
        }
        /// <summary>
        /// 顶级域名的存储
        /// </summary>
        static CSRedisClient _clientDB0 { get; set; }
        /// <summary>
        /// 关键词消息队列
        /// </summary>
        static CSRedisClient _clientDB1 { get; set; }
        /// <summary>
        /// 查询结果的消息队列
        /// </summary>
        static CSRedisClient _clientDB2 { get; set; }
        /// <summary>
        /// 关键词过期时间的配置文件及关键词
        /// </summary>
        static CSRedisClient _clientDB3 { get; set; }
        /// <summary>
        /// 
        /// </summary>
        static CSRedisClient _clientDB4 { get; set; }
        static CSRedisClient _clientDB5 { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="db">仓库</param>
        /// <returns></returns>
        public CSRedisClient CSRedisDB(RedisDb db)
        {
            switch (db)
            {
                case RedisDb.DB0:
                    if (_clientDB0 == null)
                    {
                        _clientDB0 = new CSRedisClient(_redisCon + "defaultDatabase=0");
                    }
                    return _clientDB0;
                case RedisDb.DB1:
                    if (_clientDB1 == null)
                    {
                        _clientDB1 = new CSRedisClient(_redisCon + "defaultDatabase=1");
                    }
                    return _clientDB1;
                case RedisDb.DB2:
                    if (_clientDB2 == null)
                    {
                        _clientDB2 = new CSRedisClient(_redisCon + "defaultDatabase=2");
                    }
                    return _clientDB2;
                case RedisDb.DB3:
                    if (_clientDB3 == null)
                    {
                        _clientDB3 = new CSRedisClient(_redisCon + "defaultDatabase=3");
                    }
                    return _clientDB3;
                case RedisDb.DB4:
                    if (_clientDB4 == null)
                    {
                        _clientDB4 = new CSRedisClient(_redisCon + "defaultDatabase=4");
                    }
                    return _clientDB4;
                case RedisDb.DB5:
                    if (_clientDB5 == null)
                    {
                        _clientDB5 = new CSRedisClient(_redisCon + "defaultDatabase=5");
                    }
                    return _clientDB5;
                default:
                    if (_clientDB0 == null)
                    {
                        _clientDB0 = new CSRedisClient(_redisCon + "defaultDatabase=0");
                    }
                    return _clientDB0;
            }

        }

        public void SetRedisCon(string con)
        {
            _redisCon = con;
        }
    }
    public enum RedisDb : int
    {
        DB0,
        DB1,
        DB2,
        DB3,
        DB4,
        DB5
    }
}
